package com.lanou.beautifulwardrobe.Home.Adapter;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.DetailMobileDesc;
import com.lanou.beautifulwardrobe.entify.DetailPropsName;

import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class DetailGridViewAdapter extends BaseAdapter {
    private List<DetailMobileDesc> datas;
    private List<DetailPropsName> paraDatas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();


    public void addData(List<DetailMobileDesc> datas) {
        this.datas = datas;
        //  paraDatas.clear();
        paraDatas = null;

        notifyDataSetChanged();
    }

    public void addParaData(List<DetailPropsName> paraDatas) {
        this.paraDatas = paraDatas;
        //  datas.clear();
        this.datas = null;

        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0)
            return datas.size();
        else if (paraDatas != null && paraDatas.size() > 0)
            return paraDatas.size();
        else return 0;
    }

    @Override
    public Object getItem(int position) {
        if (datas != null && datas.size() > 0)
            return datas.get(position);
        else if (paraDatas != null && paraDatas.size() > 0)
            return paraDatas.get(position);
        else return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (datas != null && datas.size() > 0) {
            DetailViewHolder holder = null;
            if (convertView == null||convertView.getTag(R.id.tag_first)==null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_product_detail_item, null);
                holder = new DetailViewHolder();
                holder.productDetailIv = (ImageView) convertView.findViewById(R.id.productDetailIv);

                convertView.setTag(R.id.tag_first, holder);

            } else {
                holder = (DetailViewHolder) convertView.getTag(R.id.tag_first);
            }
            if (datas != null && datas.size() > 0) {
                DetailMobileDesc desc = datas.get(position);

                getBitmap(desc.getContent(), holder.productDetailIv);
                Log.i("lanou", desc.getContent() + "/////");
            }

            return convertView;
        } else if (paraDatas != null && paraDatas.size() > 0) {
            ParameterViewHolder paraholder = null;
            if (convertView.getTag(R.id.tag_second) == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_product_parameter_item, null);
                paraholder = new ParameterViewHolder();


                paraholder.parapname = (TextView) convertView.findViewById(R.id.parapname);
                paraholder.paravname = (TextView) convertView.findViewById(R.id.paravname);

                convertView.setTag(R.id.tag_second, paraholder);

            } else {
                paraholder = (ParameterViewHolder) convertView.getTag(R.id.tag_second);
            }
            if (paraDatas != null && paraDatas.size() > 0) {
                DetailPropsName propsName = paraDatas.get(position);
                paraholder.parapname.setText(propsName.getPname());
                paraholder.paravname.setText(propsName.getVname());
            }
            return convertView;
        }
        return null;
    }

    class DetailViewHolder {
        private ImageView productDetailIv;

    }

    class ParameterViewHolder {
        private TextView parapname, paravname;

    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;

    @Override
    public int getItemViewType(int position) {
        if (datas != null && datas.size() > 0)
            return TYPE0;
        else
            return TYPE1;
    }

    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}